package org.ucas.kgraph.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 词条定义类
 * @author suemi
 */
@Table
@Entity
public class Term extends IdEntity {

  private static final String SEPERATOR = ";";

  @Column//(unique = true)
  private String definition;
  
  @Column//(unique = true)
  private String direction;
  
  @Column//(unique = true)
  private Float cost;
  
  @Column(columnDefinition = "Text")
  private String description;

  @Column(columnDefinition = "Text")
  private String tags; // 序列化后的tagList,采用properties的格式
  
  @Transient
  private List<String> tagList = new ArrayList<>();

  private Long rootId ; // 顶级词条默认rootId为-1
  private Long file_id ;
  
  @Column(nullable = true)
  private Long documentId;

  private Integer level = -1; // level=min(parent's level)+1
  
  @Column(nullable = true)
  private String theme;
  
  public String getTheme() {
	return theme;
  }

  public void setTheme(String theme) {
	this.theme = theme;
  }
  
  public Long getFile_id() {
	return file_id;
  }

  public void setFile_id(Long file_id) {
	this.file_id = file_id;
  }

  public String getDefinition() {
    return definition;
  }

  public void setDefinition(String definition) {
    this.definition = definition;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public List<String> getTagList(){
    if(tagList.size() == 0){
      tagList = Arrays.asList(tags.split(SEPERATOR));
    }
    return tagList;
  }

  public void setTagList(List<String> tags){
    this.tagList = tags;
    this.tags = String.join(SEPERATOR,tags);
  }

  /**
   * 对于一些使用json等复杂数据格式存储的属性在存储前做同步
   */
  public void sync(){
    this.tags = String.join(SEPERATOR,tagList);
  }

  public void addTag(String tag){
    if(tagList==null){
      tagList = Arrays.asList(tags.split(SEPERATOR));
    }
    if(!tagList.contains(tag)) tagList.add(tag);
  }

  public void removeTag(String tag){
    if(tagList==null){
      tagList = Arrays.asList(tags.split(SEPERATOR));
    }
    tagList.remove(tag);
  }

  public Float getCost() {
	return cost;
  }

  public void setCost(Float cost) {
	this.cost = cost;
  }

  public String getTags() {
    return tags;
  }

  public void setTags(String tags) {
    this.tags = tags;
  }

  public Long getRootId() {
    return rootId;
  }

  public void setRootId(Long rootId) {
    this.rootId = rootId;
  }

  public Long getDocumentId() {
    return documentId;
  }

  public void setDocumentId(Long documentId) {
    this.documentId = documentId;
  }

  public Integer getLevel() {
    return level;
  }

  public void setLevel(Integer level) {
    this.level = level;
  }

  public String getDirection() {
	return direction;
  }

  public void setDirection(String direction) {
	this.direction = direction;
  }
  
}
